/*
 * @lc app=leetcode.cn id=13 lang=javascript
 *
 * [13] 罗马数字转整数
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
    const remanMap = new Map();
    remanMap.set('I', 1)
    remanMap.set('V', 5)
    remanMap.set('X', 10)
    remanMap.set('L', 50)
    remanMap.set('C', 100)
    remanMap.set('D', 500)
    remanMap.set('M', 1000)
    let opArr = []
    let total = 0
    let old = ''
    let flag = ''
    const opObj = {}
    if (!opObj.index) {
        opObj.index = 0
    } else {
        ++opObj.index
    }
    const index = opObj.index
    const reverseArr = s.split('').reverse()
    reverseArr.forEach((value, i) => {
        if (i !== 0 && i < index) false
        let newValue = remanMap.get(value)
        let oldValue = old ? remanMap.get(old) : 0
        let flagValue = flag ? remanMap.get(flag) : 0
        if (flagValue <= newValue) {
            total += newValue
            opArr = []
            flag = value
        } else {
            // 暂时先不考虑减的逻辑
            let subtract = romanToInt(value)
            total -= subtract
        }
        old = value
        return true
    })
    return total

};
// @lc code=end

